home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 6.9 KB | 237 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWExtMgr.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFrameW.hpp"
-
- #ifndef FWEXTMGR_H
- #include "FWExtMgr.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _ISOSTR_
- #include "ISOStr.h"
- #endif
-
- #ifndef SOM_ODExtension_xh
- #include "Extensn.xh"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment extensionmanager
- #endif
-
- FW_DEFINE_AUTO(FW_CExtensionManager)
-
- #ifdef FW_USE_TEMPLATE_PRAGMAS
-
- #pragma template_access public
- #pragma template FW_TMap<const char*, FW_SPrivExtension>
- #pragma template FW_TPair<const char*, FW_SPrivExtension>
-
- #endif
-
- //========================================================================================
- // Key Matching Function
- //========================================================================================
-
- static int FW_PrivCompareExtensions(void* p1, void* p2);
-
- int FW_PrivCompareExtensions(void* p1, void* p2)
- {
- FW_CPrivNameToExtensionPair* pair1 = (FW_CPrivNameToExtensionPair*)p1;
- FW_CPrivNameToExtensionPair* pair2 = (FW_CPrivNameToExtensionPair*)p2;
-
- return ODISOStrCompare((ODISOStr)pair1->fKey, (ODISOStr)pair2->fKey);
- }
-
- //========================================================================================
- // class FW_CExtensionManager
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CExtensionManager::FW_CExtensionManager
- //----------------------------------------------------------------------------------------
-
- FW_CExtensionManager::FW_CExtensionManager(FW_CPart* part) :
- fPart(part),
- fNameToExtensionMap(NULL)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CExtensionManager::~FW_CExtensionManager
- //----------------------------------------------------------------------------------------
-
- FW_CExtensionManager::~FW_CExtensionManager()
- {
- FW_START_DESTRUCTOR
- delete fNameToExtensionMap;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CExtensionManager::HasExtension
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CExtensionManager::HasExtension(Environment* ev, const char* name)
- {
- FW_UNUSED(ev);
- return (fNameToExtensionMap ? fNameToExtensionMap->Find(name) != 0 : FALSE);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CExtensionManager::RegisterExtension
- //----------------------------------------------------------------------------------------
-
- void FW_CExtensionManager::RegisterExtension(Environment* ev,
- const char* name,
- CreateExtensionFunc createFunc,
- void* refCon,
- FW_Boolean cacheWhenReleased)
- {
- FW_UNUSED(ev);
- if (!fNameToExtensionMap)
- fNameToExtensionMap = new FW_CPrivNameToExtensionMap(FW_PrivCompareExtensions);
-
- FW_ASSERT(!fNameToExtensionMap->Find(name));
-
- fNameToExtensionMap->Add(name, FW_SPrivExtension(createFunc, refCon, cacheWhenReleased));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CExtensionManager::AcquireExtension
- //----------------------------------------------------------------------------------------
-
- ODExtension* FW_CExtensionManager::AcquireExtension(Environment*ev,
- const char* name,
- FW_Boolean createIt)
- {
- ODExtension* acquiredExtension = NULL;
-
- if (fNameToExtensionMap)
- {
- FW_CPrivNameToExtensionPair* pair = fNameToExtensionMap->Find(name);
-
- if (pair)
- {
- if (!pair->fValue.fExtension && createIt)
- {
- if (pair->fValue.fCreateFunction)
- {
- acquiredExtension = (pair->fValue.fCreateFunction)(ev, fPart, name, pair->fValue.fRefCon);
- pair->fValue.fExtension = acquiredExtension;
- }
- }
- else if (pair->fValue.fExtension && (createIt || pair->fValue.fExtension->GetRefCount(ev) > 0))
- {
- // cached extensions are returned if creation is enabled or
- // if the reference count is greater than zero. this makes
- // the behavior independent of caching
- acquiredExtension = pair->fValue.fExtension;
- acquiredExtension->Acquire(ev);
- }
- }
- }
-
- return acquiredExtension;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CExtensionManager::ReleaseExtension
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CExtensionManager::ReleaseExtension(Environment *ev, ODExtension *extension)
- {
- #ifndef FW_DEBUG
- FW_UNUSED(ev);
- #endif
- FW_ASSERT(extension!=NULL);
-
- FW_Boolean wasReleased = FALSE;
-
- if (extension && fNameToExtensionMap)
- {
- FW_ASSERT(extension->GetRefCount(ev) == 0);
-
- long mapEntries = fNameToExtensionMap->GetLength();
-
- for (long i = 0; i < mapEntries && !wasReleased; ++i)
- {
- FW_CPrivNameToExtensionPair* pair = fNameToExtensionMap->GetItemAt(i);
- if (pair->fValue.fExtension == extension)
- {
- wasReleased = TRUE;
- if (!pair->fValue.fCacheWhenReleased)
- {
- pair->fValue.fExtension = NULL;
- delete extension;
- }
- }
- }
- }
-
- return wasReleased;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CExtensionManager::PurgeCachedExtensions
- //----------------------------------------------------------------------------------------
-
- void FW_CExtensionManager::PurgeCachedExtensions(Environment* ev)
- {
- if (fNameToExtensionMap)
- {
- long mapEntries = fNameToExtensionMap->GetLength();
- for (long i = 0; i < mapEntries; ++i)
- {
- FW_CPrivNameToExtensionPair* pair = fNameToExtensionMap->GetItemAt(i);
- ODExtension* extension = pair->fValue.fExtension;
-
- if (pair->fValue.fCacheWhenReleased && extension && extension->GetRefCount(ev) == 0)
- {
- pair->fValue.fExtension = NULL;
- delete extension;
- }
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CExtensionManager::AbandonExtensions
- //----------------------------------------------------------------------------------------
-
- void FW_CExtensionManager::AbandonExtensions(Environment* ev)
- {
- PurgeCachedExtensions(ev);
-
- if (fNameToExtensionMap)
- {
- long mapEntries = fNameToExtensionMap->GetLength();
- for (long i = 0; i < mapEntries; ++i)
- {
- FW_CPrivNameToExtensionPair* pair = fNameToExtensionMap->GetItemAt(i);
- if (pair->fValue.fExtension)
- {
- pair->fValue.fExtension->BaseRemoved(ev);
- pair->fValue.fExtension = NULL;
- }
-
- }
- }
- }
-